package com.cloudkinto.controller.covershows;

import com.cloudkinto.service.covershows.CustomerCaseService;
import com.cloudkinto.service.covershows.ThymeleafShowCustomerService;
import com.cloudkinto.service.covershows.ThymeleafShowService;
import com.cloudkinto.service.covershows.impl.ThymeleafShowServiceImpl;
import com.cloudkinto.service.covershows.vo.CustomerCasePageRes;
import com.cloudkinto.service.covershows.vo.NewsInformationPageRes;
import com.cloudkinto.service.covershows.vo.NewsInformationReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/3/22 08:56
 **/
@Controller
@RequestMapping("/news")
public class ThymeleafShowController {
    @Autowired
    private ThymeleafShowService showService;
    @Autowired
    private ThymeleafShowCustomerService customerService;

    @RequestMapping("/showNewsInformationDetail/{id}")
    public String showNewsInformationDetail(Model model, @PathVariable("id") Integer id) {
        NewsInformationReq newsInformationReq = showService.selectDetail(id);
        model.addAttribute("newsInformationReq", newsInformationReq);
        return "newsDetail";
    }

    //新闻咨询
    @RequestMapping("/showNewsInformation/{current}/{pageSize}")
    public String showNewsInformation(Model model, @PathVariable(required = false, value = "current") String current,
                                      @PathVariable(value = "pageSize") String pageSize) {
        Map<String, String> map = new HashMap<>();
        try {
            Long.parseLong(current);
            Long.parseLong(pageSize);
        } catch (NumberFormatException e) {
            map.put("current", "1");
            map.put("pageSize", "5");
        }
        map.put("current", current);
        map.put("pageSize", pageSize);
        List<NewsInformationPageRes> newsInformationPageRes = showService.getPageList(map);
        model.addAttribute("dataMap", newsInformationPageRes);
        return "customerCaseDos";
    }


    //新闻咨询
    @RequestMapping()
    public String showNewsInformation(Model model) {
        Map<String, String> map = new HashMap<>();
        List<NewsInformationPageRes> newsInformationPageRes = showService.getPageList(map);
        List<CustomerCasePageRes> customerCasePageRes = customerService.getPageList(map);
        model.addAttribute("dataMap", customerCasePageRes);
        if (newsInformationPageRes.size() > 0) {
            model.addAttribute("left", newsInformationPageRes.get(0));
            if (newsInformationPageRes.size() > 4) {
                model.addAttribute("right", newsInformationPageRes.subList(1,4));
            }else {
                model.addAttribute("right", newsInformationPageRes);
            }
        }
        return "news";
    }
}
